﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;

namespace SPSocialUtil.SocialColleague
{
    /// <summary>
    /// 관심인(동료) 정보를 사용하는데 도움을 준다
    /// 하는일
    /// - 관심인을 추가한다
    /// - 관심인을 삭제한다
    /// - 관심인 목록을 반환한다
    /// </summary>
    public class ColleagueUtil
    {
        string siteUrl;
        string cUserLoginName;

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="MyUser">관심인 정보의 대상자</param>
        /// <param name="siteCollUrl">사이트 콜렉션 URL</param>
        public ColleagueUtil(SPUser myUser, string siteCollUrl)
        {
            cUserLoginName = myUser.LoginName;
            siteUrl = siteCollUrl;
        }

        public void addColleague(string colleagueLoginID)
        {
            using (SPSite site = new SPSite(siteUrl))
            {
                SPServiceContext sContext = SPServiceContext.GetContext(site);
                UserProfileManager uManager = new UserProfileManager(sContext);

                UserProfile uProfile = uManager.GetUserProfile(cUserLoginName);
                UserProfile cProfile = uManager.GetUserProfile(colleagueLoginID);

                if (uProfile.Colleagues.IsColleague(cProfile.ID)) return;

                uProfile.Colleagues.Create(cProfile, ColleagueGroupType.UserSpecified, string.Empty, true, Privacy.Public);
                uProfile.Commit();
            }
        }

        public void removeColleague(string colleagueLoginID)
        {
            using (SPSite site = new SPSite(siteUrl))
            {
                SPServiceContext sContext = SPServiceContext.GetContext(site);
                UserProfileManager uManager = new UserProfileManager(sContext);

                UserProfile uProfile = uManager.GetUserProfile(cUserLoginName);
                UserProfile cProfile = uManager.GetUserProfile(colleagueLoginID);

                if (uProfile.Colleagues.IsColleague(cProfile.ID))
                {
                    uProfile.Colleagues[cProfile].Delete();
                    uProfile.Commit();
                }
            }
        }

        public string[] getColleagueList()
        {
            string[] ColleagueList;
            ColleagueList = null;

            using (SPSite site = new SPSite(siteUrl))
            {
                SPServiceContext sContext = SPServiceContext.GetContext(site);
                UserProfileManager uManager = new UserProfileManager(sContext);
                UserProfile uProfile = uManager.GetUserProfile(cUserLoginName);

                int i = 0;
                foreach (UserProfile cProfile in uProfile.Colleagues.GetCommonColleagues())
                {
                    ColleagueList.SetValue(cProfile.DisplayName, i);
                    i++;
                }
            }
            return ColleagueList;
        }

    }
}
